package scrum.gui.Employee;

import java.util.Collection;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import scrum.entity.*;



public class MyTableModelEmployee extends AbstractTableModel {
	
	
	
	private static final long serialVersionUID = 1L;
	protected String[] columnNames;
	protected Object[][] data;

	public MyTableModelEmployee(String[] cNames,Object[][] data) {
		super();
		columnNames = cNames;
		this.data = data;
	}

	@Override
	public int getColumnCount() {
		return columnNames.length;
	}


	@Override
	public int getRowCount() {
		return data.length;
	}


	@Override
	public String getColumnName(int col) {
		return columnNames[col];
	}


	@Override
	public Object getValueAt(int row, int col) {
		
		int rows = data.length;
		int columns = data.length == 0 ? 0 : data[0].length;
		
		if (0 <= row && row < rows && 0 <= col && col < columns) {
			
			return data[row][col];
		}
		
		return null;
	}

	@Override
	public Class<?> getColumnClass(int c) {
		
		Object o = getValueAt(0, c);
		return (o == null) ? Object.class : o.getClass();
	}

	
	@Override
	public void setValueAt(Object value, int row, int col) {
		data[row][col] = value;
		fireTableCellUpdated(row, col);
	}
	
	
	public void initTable(JTable table) {
		table.setCellSelectionEnabled(false);
		table.setColumnSelectionAllowed(false);
		table.setSurrendersFocusOnKeystroke(true);
		table.getColumnModel().getColumn(0).setPreferredWidth(200);
		table.getColumnModel().getColumn(1).setPreferredWidth(100);
		table.getColumnModel().getColumn(2).setPreferredWidth(100);
		table.getColumnModel().getColumn(3).setPreferredWidth(100);
		table.getColumnModel().getColumn(4).setPreferredWidth(100);
		table.setPreferredScrollableViewportSize(table.getPreferredSize());
		table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	}
	
	
	public void showData(Collection<Task> tasks){
		
		if (tasks != null) {
			
			int i = 0;
			Object[][] newData = new Object[tasks.size()][6];
			
			for (Task task : tasks){

				newData[i][0] = task.getName();
				newData[i][1] = task.getStartDate();
				newData[i][2] = task.getDueDate();
				newData[i][3] = task.getAdvance();
				newData[i][4] = task.getStatus();
				newData[i][5] = task;
				i++;
			}
			
			data = newData; 
			fireTableDataChanged();
		}
	}
}
